<?php
class LoginController extends PageController{
	public function actionIndex(){
		$request = $this->getRequest();
		
		if($request->hasParam("link")){
			$this->link = $request->link;
		}
	}
	
	public function actionLogin(){
		$request = $this->getRequest();
				
		if($request->isPost() && $request->hasPost("user_nick") && $request->hasPost("user_pass")){
			
			$session = Session::instance();
			$mUser = new Mapper_User($this->getDb());
			
			$user = $mUser->getByLogin($request->getPost("user_nick"),$request->getPost("user_pass"));
			
			if($user instanceof User){

				$session->user_id = $user->user_id;
				if(true === $request->hasPost("link")){
					list($module,$controller,$action) = explode("_",$request->getPost("link"));
					die(header("location: /{$module}/{$controller}/{$action}"));
				}
			}
		}
		
		die(header("location: /default/login"));
	}
	
	public function actionLogout(){
		Session::instance()->regenerate();
		die(header("location: /"));
	}
}